package com.bwie.doctorclient.service.impl;

import com.bwie.common.security.utils.SecurityUtils;
import com.bwie.doctorclient.domain.dto.AddDosageDTO;
import com.bwie.doctorclient.domain.po.CommonlyDosagePO;
import com.bwie.doctorclient.domain.po.CommonlyPrescriptionPO;
import com.bwie.doctorclient.domain.po.DosagePO;
import com.bwie.doctorclient.domain.vo.DosageVO;
import com.bwie.doctorclient.domain.vo.PrescriptionVO;
import com.bwie.doctorclient.mapper.OldPrescriptionsMapper;
import com.bwie.doctorclient.mapper.PrescriptionMapper;
import com.bwie.doctorclient.service.OldPrescriptionsService;
import com.bwie.system.api.model.LoginUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class OldPrescriptionsServiceImpl implements OldPrescriptionsService {
    @Autowired
    private OldPrescriptionsMapper oldPrescriptionsMapper;

    @Autowired
    private PrescriptionMapper prescriptionMapper;

    /**
     * 医生查询历史处方列表
     * @return
     */
    @Override
    public List<PrescriptionVO> getOldPrescriptions() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<PrescriptionVO> prescriptionVOList = oldPrescriptionsMapper.getOldPrescriptions(loginUser.getUserid());

        for (PrescriptionVO prescriptionVO : prescriptionVOList) {
            //根据历史处方id查询历史处方中的药品用法用量信息
            List<DosageVO> dosageVOList = oldPrescriptionsMapper.getDosageByPrescriptionId(prescriptionVO.getId());
            prescriptionVO.setDosageVOS(dosageVOList);
        }
        return prescriptionVOList;
    }

    /**
     * 设为常用处方
     * @param prescriptionId
     * @return
     */
    @Transactional
    @Override
    public void changeCommonlyPrescription(Long prescriptionId) {
        //根据id查询历史处方信息
        CommonlyPrescriptionPO commonlyPrescriptionPO = oldPrescriptionsMapper.getOldPrescriptionById(prescriptionId);
        //插入常用处方
        prescriptionMapper.addCommonlyPrescription(commonlyPrescriptionPO);
        //根据历史处方id查询药品用法用量信息
        List<DosageVO> dosageVOS = oldPrescriptionsMapper.getDosageByPrescriptionId(prescriptionId);
        List<CommonlyDosagePO> commonlyDosagePOS = new ArrayList<>();
        for (DosageVO dosageVO : dosageVOS) {
            CommonlyDosagePO commonlyDosagePO = new CommonlyDosagePO();
            BeanUtils.copyProperties(dosageVO, commonlyDosagePO);
            commonlyDosagePOS.add(commonlyDosagePO);
        }

        for (CommonlyDosagePO commonlyDosagePO : commonlyDosagePOS) {
            //插入常用处方药品用法用量
            prescriptionMapper.addDosage(commonlyDosagePO);
            //插入常用处方药品用法用量中间表
            prescriptionMapper.addDosageRelation(commonlyPrescriptionPO.getId(),commonlyDosagePO.getId());
        }
    }

    /**
     * 删除历史处方中药品用法用量
     * @param dosageId
     * @return
     */
    @Override
    public void delDosage(Long dosageId) {
        //根据id删除药品用法用量信息
        oldPrescriptionsMapper.delDosage(dosageId);
        //根据药品用法用量id删除历史处方药品用法用量中间表
        oldPrescriptionsMapper.delDosageRelationByDosageId(dosageId);
    }

    /**
     * 编辑历史药品用法用量
     * @param
     * @return
     */
    @Override
    public void updDosage(DosagePO dosagePO) {
        oldPrescriptionsMapper.updDosage(dosagePO);
    }

    /**
     * 新增历史处方药品
     * @param
     * @return
     */
    @Transactional
    @Override
    public void addDosage(AddDosageDTO addDosageDTO) {
        DosagePO dosagePO = new DosagePO();
        BeanUtils.copyProperties(addDosageDTO,dosagePO);
        oldPrescriptionsMapper.addDosage(dosagePO);
        //新增历史处方药品用法用量中间表
        oldPrescriptionsMapper.addDosageRelation(addDosageDTO.getPrescriptionId(), dosagePO.getId());
    }
}
